package thread09.thread;

class ZhangSan
{
	public void say()
	{
		System.out.println("张三对李四说：你给我画，我就把书给你");
	}
	public void get()
	{
		System.out.println("张三得到画了");
	}
}

class Lisi
{
	public void say()
	{
		System.out.println("李四对张三说：你给我书，我就把画给你");
	}
	public void get()
	{
		System.out.println("李四得到书了");
	}
}
public class SynchronizedDemo implements Runnable
{
	private static ZhangSan zs = new ZhangSan();
	private static Lisi ls = new Lisi();
	private boolean flag = false;
	
	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		// TODO Auto-generated method stub
		SynchronizedDemo s1 = new SynchronizedDemo();
		SynchronizedDemo s2 = new SynchronizedDemo();
		s1.flag = true;
		s2.flag = false;
		new Thread(s1).start();
		new Thread(s2).start();
//		new Thread(s1).start();
//		new Thread(s2).start();
	}

	public void run()
	{
		// TODO Auto-generated method stub
		if(flag)
		{
			synchronized(zs)
			{
				zs.say();
			}
			try
			{
				Thread.sleep(500);
			}
			catch(InterruptedException e)
			{
				e.printStackTrace();
			}
			synchronized(ls)
			{
				zs.get();
			}
		}else
		{
			synchronized(ls)
			{
				ls.say();
			}
			try
			{
				Thread.sleep(5000);
			}
			catch(InterruptedException e)
			{
				e.printStackTrace();
			}
			synchronized(zs)
			{
				ls.get();
			}
		}
		
	}

}
